package com.ee.chat.api.common;

import com.ee.chat.common.dao.BaseMapper;
import com.ee.chat.common.dao.SysConfigMapper;
import com.ee.chat.common.dao.UserMapper;
import com.ee.chat.common.util.ConstantsUtil;
import com.ndhutu.ee.util.DateUtil;
import com.ndhutu.ee.util.IPUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.*;

@Service
public class CommonService {
    private Logger log = LoggerFactory.getLogger(CommonService.class);

    @Resource(name = "stringRedisTemplate")
    StringRedisTemplate redisTemplate;
    @Resource
    UserMapper userMapper;
    @Resource
    BaseMapper baseMapper;
    @Resource
    SysConfigMapper sysConfigMapper;

    /**
     * 获取用户id
     * @Author Shane
     * @Date 2019/7/2517:50
     * @param
     * @return
     **/
    public Integer getUserId(String token){
        String key = ConstantsUtil.USER_TOKEN + token;
        Object userId = redisTemplate.boundHashOps(key).get("userId");
        return userId == null ? null : Integer.parseInt(userId+"");
    }

    /**
     * 判断服务器ip是否为定时任务执行的机器
     * @Author Shane
     * @Date 2019/10/3019:50
     * @param
     * @return
     **/
    public boolean getIpStatus(){
        String ips = sysConfigMapper.getValue("timer_ips");
        List<String> ipList = Arrays.asList(ips.split(","));

        String ip = IPUtil.getServerIP();
        return ipList.contains(ip);
    }

    /**
     * 处理用户消费金币数据
     * @Author Shane
     * @Date 2019/8/2 22:12
     * @return
     */
    @Transactional(rollbackFor = Exception.class)
    public void handleUserCoinConsume() throws Exception{
        Calendar now = Calendar.getInstance();

        String time = DateUtil.getCurrTimeStr(DateUtil.pattern_day);
        time += " 03:00:00";
        Date endTime = DateUtil.parse(time);
        Date beginTime;

        //统计日榜
        Calendar cal = Calendar.getInstance();
        cal.setTime(endTime);
        cal.add(Calendar.DAY_OF_YEAR, -1);
        beginTime = cal.getTime();
        List<Map<String, Object>> tops = userMapper.getUserCoinConsumeTop(beginTime, endTime);
        for(Map<String, Object> top : tops){
            top.put("way", 0);
            top.put("type", 0);
            top.put("createTime", now.getTime());
            top.put("beginTime", beginTime);
            top.put("endTime", endTime);
        }
        if(tops.size() > 0){
            baseMapper.batchAddUserTop(tops);
        }

        //统计周榜
        int week = now.get(Calendar.DAY_OF_WEEK);
        if(week == 2){  //周一
            cal.setTime(endTime);
            cal.add(Calendar.DAY_OF_YEAR, -7);
            beginTime = cal.getTime();
            tops = userMapper.getUserCoinConsumeTop(beginTime, endTime);
            for(Map<String, Object> top : tops){
                top.put("way", 0);
                top.put("type", 1);
                top.put("createTime", now.getTime());
                top.put("beginTime", beginTime);
                top.put("endTime", endTime);
            }
            if(tops.size() > 0){
                baseMapper.batchAddUserTop(tops);
            }
        }

        //统计月榜
        int month = now.get(Calendar.DAY_OF_MONTH);
        if(month == 1){  //每月1号
            cal.setTime(endTime);
            cal.add(Calendar.MONTH, -1);
            beginTime = cal.getTime();
            tops = userMapper.getUserCoinConsumeTop(beginTime, endTime);
            for(Map<String, Object> top : tops){
                top.put("way", 0);
                top.put("type", 2);
                top.put("createTime", now.getTime());
                top.put("beginTime", beginTime);
                top.put("endTime", endTime);
            }
            if(tops.size() > 0){
                baseMapper.batchAddUserTop(tops);
            }
        }
    }

    /**
     * 处理主播积分数据
     * @Author Shane
     * @Date 2019/8/2 22:12
     * @return
     */
    @Transactional(rollbackFor = Exception.class)
    public void handleHostCredit() throws Exception{
        Calendar now = Calendar.getInstance();

        String time = DateUtil.getCurrTimeStr(DateUtil.pattern_day);
        time += " 03:00:00";
        Date endTime = DateUtil.parse(time);
        Date beginTime;

        //统计日榜
        Calendar cal = Calendar.getInstance();
        cal.setTime(endTime);
        cal.add(Calendar.DAY_OF_YEAR, -1);
        beginTime = cal.getTime();
        List<Map<String, Object>> tops = userMapper.getHostCreditTop(beginTime, endTime);
        for(Map<String, Object> top : tops){
            top.put("way", 1);
            top.put("type", 0);
            top.put("createTime", now.getTime());
            top.put("beginTime", beginTime);
            top.put("endTime", endTime);
        }
        if(tops.size() > 0){
            baseMapper.batchAddUserTop(tops);
        }

        //统计周榜
        int week = now.get(Calendar.DAY_OF_WEEK);
        if(week == 2){  //周一
            cal.setTime(endTime);
            cal.add(Calendar.DAY_OF_YEAR, -7);
            beginTime = cal.getTime();
            tops = userMapper.getHostCreditTop(beginTime, endTime);
            for(Map<String, Object> top : tops){
                top.put("way", 1);
                top.put("type", 1);
                top.put("createTime", now.getTime());
                top.put("beginTime", beginTime);
                top.put("endTime", endTime);
            }
            if(tops.size() > 0){
                baseMapper.batchAddUserTop(tops);
            }
        }

        //统计月榜
        int month = now.get(Calendar.DAY_OF_MONTH);
        if(month == 1){  //每月1号
            cal.setTime(endTime);
            cal.add(Calendar.MONTH, -1);
            beginTime = cal.getTime();
            tops = userMapper.getHostCreditTop(beginTime, endTime);
            for(Map<String, Object> top : tops){
                top.put("way", 1);
                top.put("type", 2);
                top.put("createTime", now.getTime());
                top.put("beginTime", beginTime);
                top.put("endTime", endTime);
            }
            if(tops.size() > 0){
                baseMapper.batchAddUserTop(tops);
            }
        }
    }

    /**
     * 统计主播通话时间/通话次数的数据
     * @Author Shane
     * @Date 2019/11/18 20:31
     * @param
     * @return
     **/
    @Transactional(rollbackFor = Exception.class)
    public void handleHostCallTime() throws Exception{
        Calendar now = Calendar.getInstance();

        String time = DateUtil.getCurrTimeStr(DateUtil.pattern_day);
        time += " 03:00:00";
        Date endTime = DateUtil.parse(time);
        Date beginTime;

        //统计日榜
        Calendar cal = Calendar.getInstance();
        cal.setTime(endTime);
        cal.add(Calendar.DAY_OF_YEAR, -1);
        beginTime = cal.getTime();
        List<Map<String, Object>> tops = userMapper.getHostCallTimeTop(beginTime, endTime);
        for(Map<String, Object> top : tops){
            top.put("way", 2);
            top.put("type", 0);
            top.put("createTime", now.getTime());
            top.put("beginTime", beginTime);
            top.put("endTime", endTime);
        }
        if(tops.size() > 0){
            baseMapper.batchAddUserTop(tops);
        }

        //统计周榜
        int week = now.get(Calendar.DAY_OF_WEEK);
        if(week == 2){  //周一
            cal.setTime(endTime);
            cal.add(Calendar.DAY_OF_YEAR, -7);
            beginTime = cal.getTime();
            tops = userMapper.getHostCallTimeTop(beginTime, endTime);
            for(Map<String, Object> top : tops){
                top.put("way", 2);
                top.put("type", 1);
                top.put("createTime", now.getTime());
                top.put("beginTime", beginTime);
                top.put("endTime", endTime);
            }
            if(tops.size() > 0){
                baseMapper.batchAddUserTop(tops);
            }
        }

        //统计月榜
        int month = now.get(Calendar.DAY_OF_MONTH);
        if(month == 1){  //每月1号
            cal.setTime(endTime);
            cal.add(Calendar.MONTH, -1);
            beginTime = cal.getTime();
            tops = userMapper.getHostCallTimeTop(beginTime, endTime);
            for(Map<String, Object> top : tops){
                top.put("way", 2);
                top.put("type", 2);
                top.put("createTime", now.getTime());
                top.put("beginTime", beginTime);
                top.put("endTime", endTime);
            }
            if(tops.size() > 0){
                baseMapper.batchAddUserTop(tops);
            }
        }
    }

    /**
     * 统计主播接通率*通话次数的数据
     * @Author Shane
     * @Date 2019/11/18 20:31
     * @param
     * @return
     **/
    @Transactional(rollbackFor = Exception.class)
    public void handleHostHW() throws Exception{
        Calendar now = Calendar.getInstance();

        String time = DateUtil.getCurrTimeStr(DateUtil.pattern_day);
        time += " 03:00:00";
        Date endTime = DateUtil.parse(time);
        Date beginTime;

        //统计日榜
        Calendar cal = Calendar.getInstance();
        cal.setTime(endTime);
        cal.add(Calendar.DAY_OF_YEAR, -1);
        beginTime = cal.getTime();
        List<Map<String, Object>> tops = userMapper.getHostHWTop(beginTime, endTime);
        for(Map<String, Object> top : tops){
            top.put("way", 3);
            top.put("type", 0);
            top.put("createTime", now.getTime());
            top.put("beginTime", beginTime);
            top.put("endTime", endTime);
        }
        if(tops.size() > 0){
            baseMapper.batchAddUserTop(tops);
        }

        //统计周榜
        int week = now.get(Calendar.DAY_OF_WEEK);
        if(week == 2){  //周一
            cal.setTime(endTime);
            cal.add(Calendar.DAY_OF_YEAR, -7);
            beginTime = cal.getTime();
            tops = userMapper.getHostHWTop(beginTime, endTime);
            for(Map<String, Object> top : tops){
                top.put("way", 3);
                top.put("type", 1);
                top.put("createTime", now.getTime());
                top.put("beginTime", beginTime);
                top.put("endTime", endTime);
            }
            if(tops.size() > 0){
                baseMapper.batchAddUserTop(tops);
            }
        }

        //统计月榜
        int month = now.get(Calendar.DAY_OF_MONTH);
        if(month == 1){  //每月1号
            cal.setTime(endTime);
            cal.add(Calendar.MONTH, -1);
            beginTime = cal.getTime();
            tops = userMapper.getHostHWTop(beginTime, endTime);
            for(Map<String, Object> top : tops){
                top.put("way", 3);
                top.put("type", 2);
                top.put("createTime", now.getTime());
                top.put("beginTime", beginTime);
                top.put("endTime", endTime);
            }
            if(tops.size() > 0){
                baseMapper.batchAddUserTop(tops);
            }
        }
    }


}
